package com.climb.moive.controller;

import com.climb.moive.entity.Movie;
import com.climb.moive.framework.mvc.BaseController;
import com.climb.moive.service.MovieService;
import com.climb.moive.service.impl.MovieServiceImpl;
import com.climb.moive.util.CommmonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/adminMovie")
public class AdminMovieController extends BaseController {
    MovieService movieService = new MovieServiceImpl();

    @Override
    public String index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取所有电影信息列表*/
        List<Movie> movies = movieService.findAllMovie();
        /*向前端传参数*/
        req.setAttribute("movieList", movies);
        /*返回电影列表页面*/
        return "/view/admin/AdminMovieManagement.jsp";
    }

    /**
     * 跳转到增加影片页面
     */
    public String doSave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return "/view/admin/AdminMovieAdd.jsp";
    }

    /**
     * 增加影片
     */
    public String save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Movie movie = new Movie();
        try {
            /*解决文件下载问题*/
            Map<String, String[]> map = CommmonUtils.parseUploadRequest(req);
            /*重新装载对象*/
            CommmonUtils.parseBean(movie, map);
            /*解决时间格式问题由后端给出*/
            movie.setRelease_time(new Date());

        } catch (Exception e) {
            e.printStackTrace();
        }
        movieService.insert(movie);
        return index(req, resp);
    }

    /**
     * 跳转至影片详情
     */
    public String details(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取id*/
        Integer id = Integer.valueOf(req.getParameter("id"));
        /*查找movie对象并返回*/
        req.setAttribute("movie", movieService.findMovieById(id));
        return "/view/admin/AdminMovieDetail.jsp";
    }

    /**
     * 跳转到修改信息页面影片信息
     */
    public String doModify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取选中电影id*/
        Integer id = Integer.valueOf(req.getParameter("id"));
        Movie movie = movieService.findMovieById(id);
        req.setAttribute("movie", movie);
        return "/view/admin/AdminMovieDetailModify.jsp";
    }

    /**
     * 保存修改信息
     */
    public String modify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Movie movie = new Movie();
        /* 解决文件下载问题*/
        try {
            Map<String, String[]> map = CommmonUtils.parseUploadRequest(req);
            /* 装载对象*/
            CommmonUtils.parseBean(movie, map);
            /*获取旧的movie对象旧的图片名称去删除*/
            Movie oldMovie = movieService.findMovieById(movie.getId());
            /*删除图片*/
            CommmonUtils.delecOldPic(oldMovie.getPicture(), req);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*保存对象*/
        movieService.update(movie);
        /*重新跳转到列表*/
        return index(req, resp);
    }

    /**
     * 删除电影
     */
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String pic = movieService.findMovieById(id).getPicture();
        /*删除电影*/
        movieService.delet(id);
        /*删除电影的图片*/
        try {
            CommmonUtils.delecOldPic(pic, req);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*重新跳转到列表*/
        return index(req, resp);
    }

    /**
     * 跳转至查找电影页面
     */
    public String doSearch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return "forward:/view/admin/AdminSearchMovie.jsp";
    }

    /**
     * 通过导演查找
     */
    public String searchByDirector(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取导演名字*/
        String name = req.getParameter("directorName");
        /*通过名字去模糊查找并返回列表*/
        List<Movie> movies = movieService.findMoveByDirector(name);
        req.setAttribute("movieList", movies);
        /*返回电影列表页面*/
        return "/view/admin/AdminMovieManagement.jsp";
    }

    /**
     * 通过影片名称查找
     */
    public String searchByMovieName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取影片名字*/
        String name = req.getParameter("movieName");
        /*通过名字去模糊查找并返回列表*/
        List<Movie> movies = movieService.findMoveByMovieName(name);
        req.setAttribute("movieList", movies);
        /*返回电影列表页面*/
        return "/view/admin/AdminMovieManagement.jsp";
    }

    /**
     * 通过演员查询
     */
    public String searchByActor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取影片名字*/
        String name = req.getParameter("actorName");
        /*通过名字去模糊查找并返回列表*/
        List<Movie> movies = movieService.findMoveByActor(name);
        req.setAttribute("movieList", movies);
        /*返回电影列表页面*/
        return "/view/admin/AdminMovieManagement.jsp";
    }
}
